<?php

class Jhg_Plugin_Admin extends Zend_Controller_Plugin_Abstract {

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $module = $request->getModuleName();
        
        
        
        if ('admin' === $module) {
            
            $controller = $request->getControllerName();
            if ('login' === $controller && $this->isAdmin()) {
                $this->getResponse()->setRedirect('/admin/categories');
            } elseif ('login' !== $controller && !$this->isAdmin()) {
                $this->getResponse()->setRedirect('/admin/login');
            }
        }
    }
    
    private function isAdmin() {
        $auth = Zend_Auth::getInstance();
        return $auth->hasIdentity() && $auth->getIdentity()->login === 'admin';
    }

}